package com.erp.gather.controller;

import com.erp.gather.domain.bo.ErpProductAtlasTagBo;
import com.erp.gather.domain.vo.ErpProductAtlasTagVo;
import com.erp.gather.service.IErpProductAtlasTagService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 图集标签
 *
 * @author erp
 * @date 2023-03-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gather/productAtlasTag")
public class ErpProductAtlasTagController extends BaseController {

    private final IErpProductAtlasTagService iErpProductAtlasTagService;

    @GetMapping("getAllProductTag")
    public R<Object> getAllProductTag() {
        List<ErpProductAtlasTagVo> list = iErpProductAtlasTagService.queryList(new ErpProductAtlasTagBo());
        List<Map<String, Object>> tagList = list.stream().map(vo -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", vo.getAtlasTagId());
            map.put("tagName", vo.getTagName());
            map.put("pictureNumber", vo.getPictureNumber());
            return map;
        }).collect(Collectors.toList());
        return R.ok(tagList);
    }

    /**
     * 查询图集标签列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpProductAtlasTagVo> list(ErpProductAtlasTagBo bo, PageQuery pageQuery) {
        return iErpProductAtlasTagService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取图集标签详细信息
     *
     * @param atlasTagId 主键
     */
    @GetMapping("/{atlasTagId}")
    public R<ErpProductAtlasTagVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long atlasTagId) {
        return R.ok(iErpProductAtlasTagService.queryById(atlasTagId));
    }

    /**
     * 新增图集标签
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpProductAtlasTagBo bo) {
        bo.setCreateTime(new Date());
        bo.setPictureNumber(0);
        return toAjax(iErpProductAtlasTagService.insertByBo(bo));
    }

    /**
     * 修改图集标签
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpProductAtlasTagBo bo) {
        return toAjax(iErpProductAtlasTagService.updateByBo(bo));
    }

    /**
     * 删除图集标签
     *
     * @param atlasTagIds 主键串
     */
    @DeleteMapping("/{atlasTagIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] atlasTagIds) {
        return toAjax(iErpProductAtlasTagService.deleteWithValidByIds(Arrays.asList(atlasTagIds), true));
    }
}
